package hkyg.util;

import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletResponse;

public class HkygUtil {
	public static <T> Map<String,Object> dispatch(HttpServletRequest request, HttpServletResponse response, T bizService) {
		Map<String,Object> rtnMap = new HashMap<String,Object>();
		String methodStr = request.getParameter("method");
		Method actMethod = null;
		for(Method method: bizService.getClass().getMethods()) {
			if(methodStr.equals(method.getName())) {
				actMethod = method;
			}
		}
		try {
			Map<String,Object> requestMap = HkygUtil.getRequestMap(request);
			Object result = actMethod.invoke(bizService, new Object[]{requestMap});
			if(requestMap.get("total") != null) {
				rtnMap.put("total", requestMap.get("total"));
			}
			if(requestMap.get("data") != null) {
				rtnMap.put("data", requestMap.get("data"));
			}
			if(result instanceof Boolean) {
				rtnMap.put("success", result);
			} else {
				rtnMap.put("data", result);
			}
			return rtnMap;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			rtnMap.put("sucess", false);
			return rtnMap;
		} 
		
	}
	public static Map<String,Object> getRequestMap(HttpServletRequest request) {
		Map<String,Object> rtn = new HashMap<String,Object>();
		Enumeration<String> pNames = request.getParameterNames();
		while(pNames.hasMoreElements()) {
			String pName = pNames.nextElement();			
			rtn.put(pName, request.getParameter(pName));
		}
		return rtn;
	}
	

	


	private static ThreadLocal<Map<String,Object>> threadLoal = new ThreadLocal<Map<String,Object>>();
	private static HttpSession getSession(){
		//RequestContextHolder 要依赖于 org.springframework.web.context.request.RequestContextListener
		Object reqObj = RequestContextHolder.getRequestAttributes();
		HttpSession session = null;
		if(reqObj!=null){
			//在Web环境下才有
			HttpServletRequest request = ((ServletRequestAttributes)reqObj).getRequest();
			session =  request.getSession();
		}else{
			//非Web环境下只能模拟实现
			Map<String,Object> attrMap =threadLoal.get();
			if(attrMap==null){
				attrMap = new HashMap<String,Object>();
				threadLoal.set(attrMap);
			}
		}
		return session;
	}
	public static Object getAttribute(String attrName){
		//从当前HttpSession中取值
		HttpSession httpSession=getSession();
		if(httpSession!=null)
			return httpSession.getAttribute(attrName);
		return threadLoal.get().get(attrName);
	}
	public static void setAttribute(String attrName,Object attr){
		//从当前HttpSession中设值
		HttpSession httpSession=getSession();
		if(httpSession!=null)
			httpSession.setAttribute(attrName, attr);
		else
			threadLoal.get().put(attrName, attr);
	}
	public static void removeAttribute(String attrName){
		//从当前HttpSession中移除值
		HttpSession httpSession=getSession();
		if(httpSession!=null)
			httpSession.removeAttribute(attrName);
		else
			threadLoal.get().remove(attrName);
	}
	/**
	 * 处理map忠的password对其进行MD5加密
	 * @author 杨笑天
	 */
	public static void md5Psd(Map<String,Object> params) {
		String password = (String)params.get("password");
		if(password.length() != 32) {
			params.put("password", MD5Util.MD5(password));
		}
	}
}
